from sqlalchemy import Column, Date, Float, Integer, String, ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, mapper, relation, sessionmaker
 
Base = declarative_base()
 
########################################################################
class Symbol(Base):
    """"""
    __tablename__ = "symbols"
 
    name = Column(String, primary_key=True)
    fullname = Column(String)
 
    #----------------------------------------------------------------------
    def __init__(self, name, fullname):
        """Constructor"""
        self.name = name
        self.fullname = fullname
 
    def __repr__(self):
        return "<Symbol('%s','%s')>" % (self.name, self.fullname)
 
########################################################################
class DailyData(Base):
    """
    Daily data about a Symbol database
 
    Create some class properties before initilization
    """
    __tablename__ = "daily_data"
    date = Column(Date, primary_key=True)
    symbol_name = Column(String, ForeignKey('symbols.name'), primary_key=True)

    nominalBuyAmmount = Column(Integer, nullable=False)
    nominalSellAmmount = Column(Integer, nullable=False)
    amountOfOperations = Column(Integer, nullable=False)
    
    buyPrice = Column(Float, nullable=False)
    sellPrice = Column(Float, nullable=False)
    

    symbol = relation(Symbol, backref=backref('daily_data', order_by=date))
 
    #----------------------------------------------------------------------
    def __init__(self, symbol, date):
        """Constructor"""
        self.symbol = symbol
        self.date = date


 
    def __repr__(self):
        return "<DailyData('%s, %s')>" % (self.date, self.symbol_name)
 
 
if __name__ == "__main__":
    engine = create_engine("sqlite:///bolsar.db", echo=False)
    metadata = Base.metadata
    metadata.create_all(engine)

